package com.fit.pages.product;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tapestry5.util.EnumSelectModel;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.manufacturer.ManufacturerDAO;
import com.fit.dao.product.ProductDAO;
import com.fit.dao.user.UserDAO;
import com.fit.entities.Manufacturer;
import com.fit.entities.Product;
import com.fit.entities.User;
import com.fit.model.glov.GenericSelectModel;
import com.fit.model.type.ProductType;
import com.fit.model.type.UserType;
import com.fit.model.user.MD5;

@AdminAccess
public class InsertProduct {

	@Property
	@SessionState(create = false)
	private Product product;
	
	@Inject
	private ProductDAO productDAO;
	
	@Inject
	private Messages messages;

	@Inject
	private PropertyAccess access;

	@Property
	private Manufacturer manufacturer;

	@Inject
	private ManufacturerDAO manufacturerDAO;

	@Property
	@Persist
	private ProductType type;
	
	@Component
	private Form createForm;

	@Persist
	@Property
	private String error;
	
	@Persist
	@Property
	private String confirmation;
	

	private GenericSelectModel<Manufacturer> manufacturers; // lista koja ce da se
													// prikaza u cb

	void onPrepare() {
		product = new Product();
	}

	// geter za kolekciju koja se prikazuje

	public GenericSelectModel<Manufacturer> getManufacturers() { 
		manufacturers = new GenericSelectModel<Manufacturer>(manufacturerDAO.findAll(),
				Manufacturer.class, "company", "id", access);
		return manufacturers;
	}

	public SelectModel getTypes() {
		return new EnumSelectModel(ProductType.class, messages);
	}

	
	Object onSubmitFromCreateForm() {
		error="";
		confirmation="";
		Product temp = productDAO.findById(product.getId());
		if (temp == null) {
			if (type == null) {
				error = "You didn't choose type!";
				return null;
			}
			product.setType(type.toString());
			product.setManufacturer(manufacturer);
			if(product.getManufacturer()==null) {
				Long l = new Long("3");
				product.setManufacturer(manufacturerDAO.findById(l));
			}
//			user.setPassword(MD5.md5(user.getPassword()));
			if (productDAO.save(product)) {
				confirmation = "Product successfully added!";
				return null;
			}
			confirmation = "Product is null!";
			return null;
		}
		type=null;
		error = "Product \"" + product.getName() + " " + product.getType() + " " + product.getModel()
				+ "\" already exists!";
		return null;
	}
	
	@OnEvent(component = "ResetConfLink")
	Object onResetConfLink() {
		confirmation="";
		return null;
	}
	
	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		error="";
		return null;
	}
	
	
	public boolean isErrorNotNull(){
		if(error==null || error==""){
			return false;
		}
		return true;
	}
	
	public boolean isConfirmationNotNull(){
		if(confirmation==null || confirmation==""){
			return false;
		}
		return true;
	}

}
